package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.WorkOrder;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.CodeConst;
import cn.net.shuxing.yifu.common.dto.CommitOrderReq;
import cn.net.shuxing.yifu.common.dto.HistoryWorkOrderResp;
import cn.net.shuxing.yifu.common.service.WorkOrderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/back/workOrder")
public class BackWorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    /**
     * 获取所有工单
     */
    @RequestMapping("/all")
    public BaseDto getAllWorkOrder(CommitOrderReq commitOrderReq){
        List<WorkOrder> all = workOrderService.getAll();
        BaseDto baseDto = new BaseDto();
        PageHelper.startPage(commitOrderReq.getPageNum(), commitOrderReq.getPageSize());
        baseDto.setData(new PageInfo<>(all));
        return baseDto;
    }

    /**
     * 处理完工单更改状态
     * @param id 工单Id
     */
    @RequestMapping("/getHandle")
    public BaseDto getHandle(CommitOrderReq commitOrderReq){
        BaseDto baseDto = new BaseDto();
        WorkOrder workOrder = workOrderService.getByPrimaryKey(commitOrderReq.getId());
        workOrder.setStatus(1);
        workOrderService.update(workOrder);
        baseDto.setCode(CodeConst.OK);
        return baseDto ;
    }

    /**
     * 根据用户id 查找工单
     * @param userId
     * @return
     */
    @RequestMapping("/myWorkOrder")
    public BaseDto getMyWorkOrder(CommitOrderReq commitOrderReq){
        List<HistoryWorkOrderResp> historyOrder = workOrderService.getHistoryOrder(commitOrderReq.getUserId(),commitOrderReq.getPageNum(),commitOrderReq.getPageSize());
        BaseDto baseDto = new BaseDto();
        PageHelper.startPage(commitOrderReq.getPageNum(), commitOrderReq.getPageSize());
        baseDto.setData(new PageInfo<>(historyOrder));
        return baseDto;
    }

    @RequestMapping("/search")
    public BaseDto searchWorkOrder(CommitOrderReq commitOrderReq){
        BaseDto baseDto = new BaseDto();
        PageHelper.startPage(commitOrderReq.getPageNum(), commitOrderReq.getPageSize());
        baseDto.setData(new Page<>());
        return baseDto;
    }


}
